<?php

defined('BASEPATH') or die('forbidden');

class Config_Controller Extends Controller {

    function _init() {
        check_level(50, segment(0), segment(1));
    }

    function index() {
        $dirname = array(
            'application' => translate('APPLICATION'),
            'admin' => translate('SHUIHU2012'),
        );
        $applist = array();
        $rootdir = dirname(BASEPATH);
        $dirlist = scandir($rootdir);
        $ip_whitelist = array();
        foreach ($dirlist as $m => $n) {
            $t = $rootdir . '/' . $n;
            if ($n != '.' && $n != '..' && is_dir($t) && is_file($t . '/' . '.htaccess') && is_file($t . '/' . 'index.php')) {
                $applist[$n] = isset($dirname[$n]) ? $dirname[$n] : $n;
                if ($n != 'application') {
                    if (isset($dirname[$n])) {
                        include $this->get_app_configpath($n);
                        if (isset($this->config['ip_whitelist']) && $n == 'admin') {
                            $ip_whitelist[$n] = $this->config['ip_whitelist'];
                            unset($this->config['ip_whitelist']);
                        }
                        if (isset($config['ip_whitelist'])) {
                            $ip_whitelist[$n] = $config['ip_whitelist'];
                        }
                    }
                }
            }
        }
        $this->config['ip_whitelist'] = $ip_whitelist;
        $data = array('config' => $this->config, 'applist' => $applist, 'timezonelist' => $this->form->select("timezone", get_timezone_list(), $this->config['timezone']));
        $this->view->display("general/config/index", $data);
    }

    function edit() {
        include CONFIGPATH . 'config.php';
        if (is_submit()) {
            $oldconfig = $config;
            $config['base_url'] = '';
            $config['index_page'] = 'index.php';
            $config = array_merge($config, $_POST);
            $ip_whitelist = $config['ip_whitelist'];
            $config['url_search'] = array_unique(array_filter(explode("\r\n", stripslashes($config['url_search']))));
            $config['url_replace'] = explode("\r\n", stripslashes($config['url_replace']));
            $config['allow_version'] = array_unique(array_filter(explode("\r\n", $config['allow_version'])));
            $config['deny_version'] = array_unique(array_filter(explode("\r\n", $config['deny_version'])));
            $config['forbidden_nickname'] = implode("|", array_unique(array_filter(explode('|', str_replace("\r\n", '|', $config['forbidden_nickname'])))));
            $config['forbidden_deviceid'] = implode("|", array_unique(array_filter(explode('|', str_replace("\r\n", '|', $config['forbidden_deviceid'])))));
            unset($config['db'], $config['ip_whitelist']);
            $content = mysql_real_escape_string(json_encode($config));
            $this->db->query("REPLACE INTO {pre}config(name,content) VALUES('setting','{$content}')");
            ob_start();
            echo "<?php \ndefined('BASEPATH') or die('forbidden');\n\$config = " . array2val($config) . ";";
            $content = ob_get_contents();
            ob_clean();
            @file_put_contents($this->get_app_configpath('application'), $content);
            foreach ($ip_whitelist as $m => & $n) {
                $n = array_unique(array_filter(explode("\r\n", $n)));
                $n = $n ? $n : array('127.0.0.1');
                $filename = $this->get_app_configpath($m);
                if (is_file($filename)) {
                    ob_start();
                    echo "<?php \ndefined('BASEPATH') or die('forbidden');\ninclude CONFIGPATH . 'config.php';\n\$config['ip_whitelist'] = " . array2val($n) . ";";
                    $content = ob_get_contents();
                    ob_clean();
                    @file_put_contents($filename, $content);
                }
            }
            insert_log(translate('GENERAL_CONFIG_MODIFY'), $oldconfig, $config);
            if (extension_loaded('apc') && ini_get('apc.enabled')) {
                apc_clear_cache("opcode");
            }
            $syncconfig = array('host_url' => '', 'sitename' => '', 'res_url' => '', 'timezone' => '');
            $syncconfig = array_intersect_key($config, $syncconfig);
            $syncconfig['ip_whitelist'] = $ip_whitelist;
            success(translate('C_TIPS_SUCCESS_TEXT'), site_url("general/config"));
        }
    }

    function restore() {
        $row = $this->db->one("SELECT * FROM {pre}config WHERE name='setting'");
        $config = obj2array(json_decode($row['content']));
        $config = $this->input->deep_stripslashes($config);
        ob_start();
        echo "<?php \ndefined('BASEPATH') or die('forbidden');\n\$config = ";
        echo array2val($config);
        echo ";";
        $content = ob_get_contents();
        ob_clean();
        @file_put_contents($this->get_app_configpath('application'), $content);
        insert_log(translate('GENERAL_CONFIG_RECOVER'));
        success(translate('C_TIPS_SUCCESS_TEXT'), site_url("general/config"));
    }

    function get_app_configpath($app) {
        $name = '/application/configs/config.php';
        if ($app != 'application') {
            $name = '/' . $app . $name;
        }
        return dirname(BASEPATH) . $name;
    }

}